package c2.group.business.tb;

import c2.group.business.base.GroupSubModule;
import cate.game.play.part.PlaySide;
import cate.game.pvp.throne.msg.*;
import cate.game.util.GameKit;
import cp.solution.rpc.RpcMsgHead;
import easy.java.dev.note.NoteClass;
import easy.java.practice.errorcode.EcResult;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@NoteClass("王座争霸-group消息接受模块")
public class TBGroupReceiveModule extends GroupSubModule<TBModule> {

    @Override
    public void initDependent() throws Exception {
        super.initDependent();
        useRpcRecv().onFireAndForgetTemplate(TBInnerMsg.GAME_CONNECT, Integer.class, this::onGameConnect);
        useRpcRecv().onRequestResponseTemplate(TBInnerMsg.TRIAL_OPPONENT_REQ, TBTrialOpponentReq.class, this::refreshOpponent);
        useRpcRecv().onFireAndForgetTemplate(TBInnerMsg.TRIAL_SYNC_RANK_REQ, TBTrialRankSyncReq.class, this::syncTrialRank);
        useRpcRecv().onRequestResponseTemplate(TBInnerMsg.TRIAL_DATA_REQ, TBTrialInnerReq.class, this::getTrialData);

        useRpcRecv().onFireAndForgetTemplate(TBInnerMsg.UPDATE_KO_POSITION, TBKoSyncPositionReq.class, this::updateKoPosition);
        useRpcRecv().onRequestResponseTemplate(TBInnerMsg.PLAY_SIDE_REQ, TBKoPlaySideInnerReq.class, this::getPlaySide);
        useRpcRecv().onRequestResponseTemplate(TBInnerMsg.KO_BET_REQ, TBKoBetInnerReq.class, this::koBet);

        useRpcRecv().onRequestResponseTemplate(TBInnerMsg.BOSS_CHECK, TBBossCheckInnerReq.class, this::bossCheck);
        useRpcRecv().onRequestResponseTemplate(TBInnerMsg.BOSS_RESULT, TBBossResultInnerReq.class, this::bossResult);


        useRpcRecv().onFireAndForgetTemplate(TBInnerMsg.GM_ADD_RIVAL_ROBOT_REQ, TBGMAddRivalRobotReq.class, this::gmAddRivalRobot);
        useRpcRecv().onFireAndForgetTemplate(TBInnerMsg.GM_ADD_TRIAL_ROBOT_REQ, TBGMAddTrialRobotReq.class, this::gmAddTrialRobot);
    }

    /**
     * 游戏服连接
     * @param head  头信息
     * @param srvId 服务器ID
     */
    public void onGameConnect(RpcMsgHead head, int srvId) {
        parent.markNeedNotice();
        log.info("游戏服[{}]请求下发[王座争霸]基础数据", srvId);
    }

    public TBTrialOpponentInnerResp refreshOpponent(RpcMsgHead head, TBTrialOpponentReq req){
        return parent.trial.getTrialOpponent(req);
    }

    public void syncTrialRank(RpcMsgHead head, TBTrialRankSyncReq req){
        parent.trial.syncTrialRank(req);
    }

    public TBTrialInnerResp getTrialData(RpcMsgHead head, TBTrialInnerReq req){
        return parent.trial.getTrialData(req);
    }

    public void updateKoPosition(RpcMsgHead head, TBKoSyncPositionReq req){
        parent.ps.setPlaySide(req.playSide);
    }

    public TBKoPlaySideInnerResp getPlaySide(RpcMsgHead head, TBKoPlaySideInnerReq req) {
        return TBKoPlaySideInnerResp.of(parent.ps.getPlaySide(req.roleUid));
    }

    public TBKoBetNotice koBet(RpcMsgHead head, TBKoBetInnerReq req){
        return parent.ko.bet(req.betTeam);
    }

    public TBBossCheckInnerResp bossCheck(RpcMsgHead head, TBBossCheckInnerReq req) {
        TBBossCheckInnerResp resp = new TBBossCheckInnerResp();
        resp.result = parent.boss.challengeCheck(req.roleUid,req.heroList);
        return resp;
    }

    public TBBossResultInnerResp bossResult(RpcMsgHead head, TBBossResultInnerReq req) {
        TBBossResultInnerResp resp = new TBBossResultInnerResp();
        EcResult<Void> r = parent.boss.fightReport(req);
        if (r.ok()) {
            parent.message.noticeBoss(parent.boss.getStatus());
        }
        resp.result = r;
        return resp;
    }


    public void gmAddRivalRobot(RpcMsgHead head, TBGMAddRivalRobotReq req) {
        log.info("游戏服请求gm添加对手库机器人");
        List<PlaySide> playSides = new ArrayList<>();
        for (int i = 0; i < req.num; i++) {
            PlaySide ps = req.playSide.copy();
            String fakeUid = GameKit.uid();
            ps.snapshot.uid = fakeUid;
            ps.snapshot.base.uid = fakeUid;
            ps.snapshot.base.srvId = -1;
            ps.snapshot.base.name =  "gm机器人" + (i + 1) + "号";
            ps.snapshot.base.headIcon.tid = 1001 + (int) (Math.random() * 88);
            ps.snapshot.base.avatar.tid = 1001 + (int) (Math.random() * 88);
            playSides.add(ps);
        }
        parent.rival.gmAddRivals(req.levelGroup, playSides);
    }


    public void gmAddTrialRobot(RpcMsgHead head, TBGMAddTrialRobotReq req) {
        log.info("游戏服请求gm添加选拔赛机器人");
        for (int i = 0; i < req.num; i++) {
            PlaySide ps = req.playSide.copy();
            String fakeUid = GameKit.uid();
            ps.snapshot.uid = fakeUid;
            ps.snapshot.base.uid = fakeUid;
            ps.snapshot.base.srvId = -1;
            ps.snapshot.base.name = "gm机器人" + (i + 1) + "号";
            ps.snapshot.base.headIcon.tid = 1001 + (int) (Math.random() * 88);
            ps.snapshot.base.avatar.tid = 1001 + (int) (Math.random() * 88);
            TBTrialRankSyncReq fakeReq = new TBTrialRankSyncReq();
            fakeReq.point = 5001;
            fakeReq.levelGroup = req.levelGroup;
            fakeReq.playSide = ps;
            parent.trial.syncTrialRank(fakeReq);
        }
    }
}
